import apis from "../services/AddSourceApi";
import toTreeData from "./getTree";
// import toSelectData from "./selectTreeData";
// import toSelTreeData from "./components/GetSelTree";

export default {
  namespace: "adddataSource",
  state: {
    errCode: "-1",
    dataDetail: {},
    errCodeMod: "-1",
    key: {},
    testErr: false,
    orgTree: [],
    selectData: [],
    errMsg: "",
    origiTree: [],
    selTree: [],
    subMsg: "",
  },
  subscriptions: {
    // 监听地址工作台
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (
          location.pathname ===
            "/intellPush/recommendCenter/dataSource/editInfo" ||
          location.pathname ===
            "/intellPush/recommendCenter/dataSource/addInfo" ||
          location.pathname ===
            "/intellPush/recommendCenter/dataSource/dataSourceDetail"
        ) {
          if (location.search.indexOf("key") >= 0) {
            const key = JSON.parse(
              window.decodeURIComponent(location.search).substring(5)
            );
            dispatch({ type: "put/putkey", payload: key });
            if (key.operate === "mod" || key.operate === "detail") {
              dispatch({ type: "dataDetail", payload: { dsId: key.dsId } });
            }
          }
        } else {
          // dispatch({
          //   type: "clearData",
          //   payload: {}
          // });
        }
      });
    },
  },
  effects: {
    *addDataSource({ payload }, { call, put }) {
      const res = yield call(apis.addDataSource, payload);
      const { errCode, errMsg } = res;
      yield put({
        type: "get/errCode",
        payload: errCode,
      });
      yield put({
        type: "put/errMsg",
        payload: errMsg,
      });
    },
    *dataDetail({ payload }, { call, put }) {
      const res = yield call(apis.dataDetail, payload);
      const { datas } = res;
      yield put({
        type: "get/detailData",
        payload: datas,
      });
    },
    *editData({ payload }, { call, put }) {
      const res = yield call(apis.editDataSource, payload);
      const { errCode, errMsg } = res;
      yield put({
        type: "get/errCodeMod",
        payload: errCode,
      });
      yield put({
        type: "put/errMsg",
        payload: errMsg,
      });
    },
    *testConnect({ payload }, { call, put }) {
      const res = yield call(apis.testConnect, payload);
      const { success } = res;
      yield put({
        type: "get/testErrCode",
        payload: success,
      });
    },
    *getOrgList({ payload }, { call, put }) {
      const { datas, errCode } = yield call(apis.orgList, payload);
      // const selTree = toSelTreeData(data);
      if (errCode === 0) {
        const orgTree = toTreeData(datas||[]);
        yield put({
          type: "get/getTree",
          payload: orgTree,
        });
        return orgTree;
      }
      return [];
    },
  },
  reducers: {
    "get/errCode": (state, { payload }) => ({
      ...state,
      errCode: payload,
    }),
    "get/detailData": (state, { payload }) => ({
      ...state,
      dataDetail: payload,
    }),
    "get/errCodeMod": (state, { payload }) => ({
      ...state,
      errCodeMod: payload,
    }),
    "put/putkey": (state, { payload }) => ({
      ...state,
      key: payload,
    }),
    "get/testErrCode": (state, { payload }) => ({
      ...state,
      testErr: payload,
    }),
    "get/getTree": (state, { payload }) => ({
      ...state,
      orgTree: payload,
    }),
    "get/getorigiTree": (state, { payload }) => ({
      ...state,
      origiTree: payload,
    }),
    "put/errMsg": (state, { payload }) => ({
      ...state,
      errMsg: payload,
    }),
    "get/getSelTreeData": (state, { payload }) => ({
      ...state,
      selTree: payload,
    }),
    // "clear/clearData": (state, { payload }) => ({
    //   ...state,
    //   key: payload
    // })
  },
};
